<?php

namespace app\admin\controller\service;

use app\common\controller\Backend;
use think\Db;
use app\admin\model\service\order\Order;
/**
 * 数据统计
 *
 * @icon fa fa-circle-o
 */
class Data extends Backend
{
    public function index()
    {
        $info = db('service_total_data')->field('total_order_nums,total_user_nums,total_plus_nums,total_shop_nums,total_skill_nums,total_price,rebate_price,recharge_price,withdraw_price,skill_ensure_price,shop_ensure_price,day_order_nums')->order('id desc')->find();
        $data = \app\api\model\service\TotalData::totalData();
        $diff = [];
        foreach ($data as $key=>$value)
        {
            $diff[$key] = $info?strstr($key,'num')?intval($value-$info[$key]):sprintf("%.2f",$value-$info[$key]):0;
        }
        $orderTotal = \app\api\model\service\Order::getTotalOrder();
        $userTotal = \app\api\model\service\User::getTotalUser();
        $this->assignconfig('day',$orderTotal['day']);
        $this->assignconfig('ordertotal',$orderTotal['total']);
        $this->assignconfig('usertotal',$userTotal);
        $starttime = strtotime(date("Y-m-d",time()));
        $endtime = $starttime+86399;
        $this->assign([
            'data'          => $data,
            'diff'          => $diff,
            'finish'        => \app\admin\model\service\order\Order::where(['finishtime'=>['between',[$starttime,$endtime]]])->where(['is_service'=>['in',[0,-1]]])->count(),
            'notFinish'     => \app\admin\model\service\order\Order::where(['paytime'=>['between',[$starttime,$endtime]]])->where(['status'=>['in',[1,2,3,4,5]],'is_service'=>['in',[0,-1]]])->count(),
            'afterNotFinish'=> \app\admin\model\service\order\Order::where(['starttime'=>['between',[$starttime+172800,$endtime+172800]]])->where(['status'=>['>',0],'is_service'=>['in',[0,-1]]])->count(),
            'beforeFinish'  => \app\admin\model\service\order\Order::where(['finishtime'=>['between',[$starttime-86400,$endtime-86400]]])->where(['status'=>['>',0],'is_service'=>['in',[0,-1]]])->count(),
            'tomorrowNotFinish' => \app\admin\model\service\order\Order::where(['starttime'=>['between',[$starttime+86400,$endtime+86400]]])->where(['is_service'=>['in',[0,-1]]])->count(),
            'refund'        => \app\admin\model\service\order\Refundorder::where('state',0)->count(),
            'dayOrder'      => \app\admin\model\service\order\Order::where(['paytime'=>['between',[$starttime,$endtime]]])->where(['is_service'=>['in',[0,-1]]])->count(),
            'dayUser'       => \app\admin\model\User::where(['jointime'=>['between',[$starttime,$endtime]]])->count(),
            'dayRecharge'   => \app\admin\model\service\money\Recharge::where(['paytime'=>['between',[$starttime,$endtime]]])->sum('price'),
            'dayGoods'      => (new Order())->getRank('goods_id','day'),
            'monthGoods'      => (new Order())->getRank('goods_id','month'),
            'yearGoods'      => (new Order())->getRank('goods_id','year'),
            'allGoods'      => (new Order())->getRank('goods_id','all'),
            'dayCity'      => (new Order())->getRank('city','day'),
            'monthCity'      => (new Order())->getRank('city','month'),
            'yearCity'      => (new Order())->getRank('city','year'),
            'allCity'      => (new Order())->getRank('city','all'),
            'dayShop'      => (new Order())->getRank('shop_id','day'),
            'monthShop'      => (new Order())->getRank('shop_id','month'),
            'yearShop'      => (new Order())->getRank('shop_id','year'),
            'allShop'      => (new Order())->getRank('shop_id','all'),
            'daySkill'      => (new Order())->getRank('skill_id','day'),
            'monthSkill'      => (new Order())->getRank('skill_id','month'),
            'yearSkill'      => (new Order())->getRank('skill_id','year'),
            'allSkill'      => (new Order())->getRank('skill_id','all'),
        ]);
        return $this->view->fetch();
    }


}
